package com.jiemengshi.server.service;

import com.jiemengshi.server.mapper.*;
import com.jiemengshi.server.model.*;
import com.jiemengshi.server.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MingQi on 2017/10/14.
 */
@Service
public class WechatDreamService {
    @Autowired
    private DreamAnalysisMapper dreamAnalysisMapper;

    @Autowired
    private DreamRecordMapper dreamRecordMapper;

    @Autowired
    private DreamReplyMapper dreamReplyMapper;

    @Autowired
    private AnnouncementMapper announcementMapper;

    @Autowired
    private LinkMapper linkMapper;
    
    public List<DreamAnalysis> analyzeDream(String keyWord) {
        List<DreamAnalysis> exactAnalyses = dreamAnalysisMapper.selectByKeyWord(keyWord);

        List<Integer> excludedIds = new ArrayList<>();
        for (DreamAnalysis analysis : exactAnalyses) {
            excludedIds.add(analysis.getId());
        }

        List<DreamAnalysis> generalAnalyses = dreamAnalysisMapper.selectFuzzyByKeyWord(keyWord, excludedIds);
        exactAnalyses.addAll(generalAnalyses);

        return exactAnalyses;
    }

    public boolean addDreamRecord(DreamRecord dreamRecord) {
        String currentTime = DateUtil.getCurrentTime();
        
        dreamRecord.setId(null);
        dreamRecord.setCreateTime(currentTime);
        dreamRecord.setModifyTime(currentTime);

        return dreamRecordMapper.insertSelective(dreamRecord) > 0;
    }
    
    public boolean updateDreamRecord(DreamRecord record) {
        record.setCreateTime(null);
        record.setModifyTime(DateUtil.getCurrentTime());

        return dreamRecordMapper.updateByPrimaryKeySelective(record) > 0;
    }

    public DreamRecord getDreamRecordById(int id) {
        return dreamRecordMapper.selectByPrimaryKey(id);
    }

    public List<DreamRecord> getDreamRecordByUserId(int wechatUserId, int start, int limit) {
        return dreamRecordMapper.selectByUserId(wechatUserId, start * limit, limit);
    }

    public int countDreamRecordByUserId(int wechatUserId) {
        return dreamRecordMapper.countByUserId(wechatUserId);
    }

    public List<DreamRecord> getDreamRecordByTitle(int wechatUserId, String title, int start, int limit) {
        return dreamRecordMapper.selectByTitle(wechatUserId, title, start * limit, limit);
    }

    public int countDreamRecordByTitle(int wechatUserId, String title) {
        return dreamRecordMapper.countByTitle(wechatUserId, title);
    }

    public boolean deleteDreamRecordById(int id) {
        return dreamRecordMapper.deleteByPrimaryKey(id) > 0;
    }
    
    public List<DreamReply> getDreamReplyByRecordId(int recordId) {
        return dreamReplyMapper.selectByRecordId(recordId);
    }

    public boolean resetDreamRecordNewReply(int recordId) {
        return dreamRecordMapper.updateNewReply(recordId, 0) > 0;
    }
    
    public boolean addDreamReply(DreamReply dreamReply) {
        dreamReply.setId(null);
        dreamReply.setCreateTime(DateUtil.getCurrentTime());

        boolean success = dreamReplyMapper.insertSelective(dreamReply) > 0;
        if (success) {
            dreamRecordMapper.updateUserReply(dreamReply.getDreamRecordId(), 1);
        }
        
        return success;
    }
    
    public boolean deleteDreamReplyById(int id) {
        return dreamReplyMapper.deleteByPrimaryKey(id) > 0;
    }

    public List<Announcement> listAnnouncement() {
        return announcementMapper.listAnnouncement();
    }
    
    @Cacheable(value = "userWechat")
    public Link getMainPageNewsLink() {
        int linkId = 1;
        return linkMapper.selectByPrimaryKey(linkId);
    }

    public List<Link> getLinksByRange(int low, int high) {
        return linkMapper.selectByIdRange(low, high);
    }
    
    public boolean dreamNeedHelp(int dreamReocrdId) {
        return dreamRecordMapper.updateNeedHelp(dreamReocrdId, 1) == 1;
    }

    public int countCurrentDayVoiceRecord(int wechatUerId) {
        String currentDate = DateUtil.getCurrentDate();
        return dreamRecordMapper.countVoiceByTime(wechatUerId, currentDate + " 00:00:00", currentDate + " 23:59:59");
    }
}
